package com.sec.android.app.myfiles.module.local.foldertree;

import android.app.FragmentManager;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.media.MediaMetadataRetriever;
import android.provider.MediaStore;

import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.module.abstraction.AbsFileOperationImp;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.shortcut.ShortcutUtils;
import com.sec.android.app.myfiles.operation.FileOperationArgs;
import com.sec.android.app.myfiles.operation.FileOperationException;
import com.sec.android.app.myfiles.operation.FileOperator;
import com.sec.android.app.myfiles.operation.FileOperator.Operation;
import com.sec.android.app.myfiles.operation.ProgressListener;
import com.sec.android.app.myfiles.provider.PrivateMyFilesProvider;
import com.sec.android.app.myfiles.util.FileUtils;
import com.sec.android.app.myfiles.util.SemFwWrapper;

import java.io.File;
import java.util.ArrayList;

public class FolderTreeOperationImp extends AbsFileOperationImp {

    public FolderTreeOperationImp(Context context, ProgressListener listener) {
        super(context, listener);
    }

    @Override
    protected void makeScanLists(Operation op, ArrayList<String> pathList, ArrayList<String> fileList, FileOperationArgs args, int handledCount) {
    }

    @Override
    protected boolean isSupport(FileRecord record) {
        return record.getStorageType() == FileRecord.StorageType.FolderTree;
    }

    @Override
    protected boolean isSupportCopyMove(FileRecord src, FileRecord dst) {
        boolean bRet = false;

        if (src != null && dst != null) {
            FileRecord.StorageType srcType = src.getStorageType();
            FileRecord.StorageType dstType = dst.getStorageType();
            if (FileRecord.StorageType.FolderTree.equals(srcType) && (FileRecord.StorageType.FolderTree.equals(dstType))) {
                bRet = true;
            }
        }
        return bRet;
    }

    @Override
    protected boolean _copy(FileRecord src, FileRecord dst) throws FileOperationException {
        // NOT USE
        return false;
    }

    @Override
    protected boolean _copy(ArrayList<FileRecord> src, FileRecord dst, FragmentManager fragmentManager, boolean singleFile, boolean dupCheck,
                            FileOperator operator) throws FileOperationException {
        return false;
    }

    @Override
    protected boolean _move(FileRecord src, FileRecord dst) throws FileOperationException {
        // NOT USE
        return false;
    }

    @Override
    protected boolean _move(ArrayList<FileRecord> src, FileRecord dst, FragmentManager fragmentManager, boolean singleFile, FileOperator operator, ArrayList<FileRecord> handledSrcList)
            throws FileOperationException {
        return false;
    }

    @Override
    protected boolean _rename(FileRecord src, FileRecord dst) throws FileOperationException {
        File fsrc = SemFwWrapper.file(src.getFullPath());
        File fdst = SemFwWrapper.file(dst.getFullPath());

        boolean bRet = fsrc.renameTo(fdst);

        if (bRet) {
            if (!src.isDirectory()) {
                updateMediaDbForRename(src, dst);
            }
            PrivateMyFilesProvider.refreshDb(mContext, dst.getParent(), true);
            ShortcutUtils.deleteShortcutFromHome(mContext, src);
        }
        return bRet;
    }

    @Override
    protected boolean _delete(FileRecord record) {
        return false;
    }

    @Override
    protected FileRecord _mkdir(FileRecord dst, String name) {
        // NOT USE
        return null;
    }

    @Override
    protected FileRecord _getUniqueFileRecord(FileRecord record) {
        return null;
    }

    @Override
    protected ArrayList<FileRecord> _getListInDirectory(FileRecord record) {
        return null;
    }

    @Override
    protected void _preOperation(Operation operationType) {
    }

    @Override
    protected void _postOperation(Operation operationType, FileOperationArgs args, int handledCount) {
    }

    @Override
    protected boolean isSupportRecursiveCopyMove() {
        return false;
    }

    private void updateMediaDbForRename(FileRecord src, FileRecord dst) {
        ContentResolver resolver = mContext.getContentResolver();

        if ((src.getId() >= 0) && src.getPath().equals(dst.getPath())) {
            ContentValues cv = new ContentValues();
            String path = dst.getFullPath();
            cv.put(MediaStore.Files.FileColumns.DATA, path);
            cv.put(MediaStore.Files.FileColumns.DISPLAY_NAME, dst.getName());
            cv.put(MediaStore.Files.FileColumns.TITLE, getTitle(dst));

            try {
                resolver.update(src.getUri(mContext), cv, null, null);
                resolver.notifyChange(FileUtils.MEDIA_PROVIDER_URI, null);
            } catch (RuntimeException e) {
                Log.e(this, "The data of db field should be UNIQUE data");
            }

        }
    }

    private String getTitle(FileRecord record) {
        String ret = record.getNameWithoutExt();

        if (record.isAudioFileType() || record.isVideoFileType()) {
            MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();
            try {
                String path = record.getFullPath();
                if (path != null) {
                    metaRetriever.setDataSource(path);
                }
                String title = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);

                if (title != null && !title.isEmpty()) {
                    ret = title;
                }
            } catch (RuntimeException e) {
                Log.e(this, "RuntimeException:" + e.toString());
            } finally {
                metaRetriever.release();
            }
        }
        return ret;
    }
}
